<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <list-table
        ref="table"
        :loading="loading"
        :statistics-config="{ use: true, statistics }"
        :pagination="false"
        :export-config="{ use: true,select: '物资招标采购申请导出选中',all: '物资招标采购申请导出全部'}"
        :columns="columns"
        :request="loadData"
        row-key="id"
      >
          <a-button slot="prefix"
            v-btnPermission="'mm_005_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>
        <span
          slot="index"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template
          slot="collect"
          slot-scope="text, record, index"
        >
          {{ text ? '是' : '否' }}
        </template>
        <template
          slot="projectName"
          slot-scope="text, record, index"
        >
          {{ text ? text : record.branchCompanyName }}
        </template>
        <template
          slot="projectCode"
          slot-scope="text, record, index"
        >
          {{ text ? text : record.branchCompanyCode }}
        </template>
        <template
          slot="auditStatusStr"
          slot-scope="text, record, index"
        >
          <list-status
            :status="record.auditStatus"
            :text="text"
          />
        </template>
        <div
          slot="operation"
          slot-scope="text, record, index"
          class="pub-table-list-action"
        >
          <span @click="handleEdit(record, 3)">查看</span>
          <span
            v-if="record.auditStatus === 'PAST'"
            @click="invalidResult(record)"
          >作废</span>
          <template v-if="record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED'">
            <span @click="() => handleEdit(record, 2)">编辑</span>
            <span @click="() => handleDel(record.id)">删除</span>
          </template>
          <approval-button
            :id="record.id"
            :status="record.auditStatus"
            v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' "
          />
        </div>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { applyList, applyListDel, invalid,findListCount } from '@/api/material/procurement/apply'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'


export default {
  name:'a'+Date.now(),
    data: function() {
        return {
            exportAllLoading: false,
            visible: false,
            loading: false,
            businessId: '',
            pageNo: 1,
            pageSize: 10,
            totalSize: 0,
            searchArr: [
                // 搜索框
                {
                    name: '所属分公司',
                    valueKey: 'branchCompanyId',
                    type: SelectSubCompany
                },
                // {
                //     name: '项目名称',
                //     type: SelectProject,
                //     valueKey: 'projectId'
                // },
                {
                    name: '招标采购申请编码',
                    value: 'code',
                    type: 'input'
                },
                {
                    name: '是否集采',
                    value: 'collect',
                    type: 'select',
                    data: [
                        { label: '是', value: 'true', enabled: true },
                        { label: '否', value: 'false', enabled: true }
                    ]
                },
                {
                    name: '编号',
                    value: 'number',
                    type: 'input'
                },
                {
                    name: '拟开标时间',
                    value: 'bidOpenDate',
                    type: 'dateRange'
                },
                {
                    name: '创建人',
                    value: 'creatorName',
                    type: 'input'
                },
                {
                    name: '创建时间',
                    value: 'createTime',
                    type: 'timeRangeFormat'
                },
                {
                    name: '状态',
                    value: 'auditStatus',
                    type: 'select',
                    data: this.STATUS
                }


            ],
            columns: [
                {
                    title: '序号',
                    dataIndex: 'index',
                    scopedSlots: { customRender: 'index' },
                    width: 70
                },
                {
                    title: '分公司',
                    dataIndex: 'branchCompanyName',
                    width: 160
                },
                {
                    title: '项目名称',
                    dataIndex: 'projectName',
                    width: 160
                },
                {
                    title: '物资招标采购申请编码',
                    dataIndex: 'code',
                    width: 180
                },
                {
                    title: '是否集采',
                    dataIndex: 'collect',
                    scopedSlots: { customRender: 'collect' },
                    width: 120
                },

                {
                    title: '编号',
                    dataIndex: 'number',
                    width: 100
                },
                {
                    title: '拟开标时间',
                    dataIndex: 'bidOpenDate',
                    width: 120
                },
                {
                    title: '创建人',
                    dataIndex: 'creatorName',
                    width: 160
                },
                {
                    title: '创建时间',
                    dataIndex: 'createTime',
                    width: 160
                },
                {
                    title: '状态',
                    dataIndex: 'auditStatusStr',
                    scopedSlots: { customRender: 'auditStatusStr' },
                    width: this.STATUS_WIDTH
                },
                {
                    title: '操作',
                    dataIndex: 'operation',
                    scopedSlots: { customRender: 'operation' },
                    width: 180,
                    fixed: 'right'
                }
            ],
            selectedRowKeys: [],
            selectId: [],
            selectedRows: [],
            chooseData: 0,
            tableData: [],
            queryParam: {},

            statistics:{},
          pathName: this.$route.path,
          loadData: async parameter => {
            this.loading = true;
             const searchData = Object.assign({}, parameter, this.queryParam)
              if (searchData.bidOpenDate) {
                searchData.bidOpenDateStart = this.formatDate(searchData.bidOpenDate[0], true)
                searchData.bidOpenDateEnd = this.formatDate(searchData.bidOpenDate[1], true)
            }
            if (searchData.collect === 1) {
                searchData.collect = true
            }
            if (searchData.collect === 0) {
                searchData.collect = false
            }
            delete searchData.createTime
            delete searchData.bidOpenDate
            this.loading = true
             findListCount(searchData).then(res => {
                if (res.code === this.SUCCESS_CODE) this.statistics = res.data
            })
             return applyList(searchData).then((res) => {
                 this.loading = false;
                 return res;
             })
            },
        }
    },
    created() {
        // this.list()
    },
    mounted: function() {
        // this.getOption();
      // 监听详情页返回刷新列表
      //this.pathName取的值是this.$route.path
      this.$bus.$on('handleRefresh' + this.pathName, () => {
        console.log('--------------------收到了，刷新刷新刷新----------------',this.pathName)
        // this.list()
      })
    },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
    methods: {
        approvalDetail(row) {
            if (!row.businessId) {
                this.$error({ content: '无有效业务ID，请刷新数据重试！' })
                return
            }
            const query = {
                taskDefKey: row.taskDefKey,
                businessType: row.businessType,
                businessId: row.businessId,
                id: row.businessId,
                procInstId: row.procInstId,
                name: encodeURIComponent(row.taskName),
                processName: encodeURIComponent(row.processName),
                taskId: row.taskId
            }
            if (val.detailUrl) {
                query['detailUrl'] = encodeURIComponent(row.detailUrl)
            }
            let routeData = this.$router.resolve({ path: row.taskPcUrl, query })
            window.open(routeData.href, '_blank')
        },
        exportSelect: function() {
            if (this.selectedRowKeys.length > 0) {
                let temp = this.$clone(this.selectedRows)
                temp.map(v => {
                    if (!v.projectName) v.projectName = v.branchCompanyName
                    if (!v.projectCode) v.projectCode = v.branchCompanyCode
                    v.collect = v.collect ? '是' : '否'
                })
                this.$refs.exportSelect.setDataAndGenerate(temp)
            } else {
                this.$message.warning('请选择需要导出的数据')
            }
        },
        exportAll: function() {
            // 参数：查询条件、导出的回调
            if (this.totalSize) {
                this.pageNo = 1
                this.exportAllLoading = true
                this.list(false, this.$refs.exportAll.setDataAndGenerate)
            } else {
                this.$message.warning('没有可导出的数据')
            }
        },
        invalidResult: function(row) {
            this.invalidList(invalid, this.search, { id: row.id })
        },
        getOption: function() {
            let _this = this
            selectOption({
                type: 11
            }).then(res => {
                if (res.code === 200) {
                    for (let i = 0; i < res.data.length; i++) {
                        _this.searchArr[1].data.push({ label: res.data[i].name, value: res.data[i].id })
                    }
                }
            })
        },
        list(params, callback) {
            let _this = this
            let searchData = Object.assign({
                pageSize: callback ? _this.totalSize : _this.pageSize,
                pageNo: _this.pageNo
            }, params, this.queryParam)
            // if (searchData.createTime) {
            //     searchData.createTimeStart = this.formatDate(searchData.createTime[0])
            //     searchData.createTimeEnd = this.formatDate(searchData.createTime[1])
            // }
            if (searchData.bidOpenDate) {
                searchData.bidOpenDateStart = this.formatDate(searchData.bidOpenDate[0], true)
                searchData.bidOpenDateEnd = this.formatDate(searchData.bidOpenDate[1], true)
            }
            if (searchData.collect === 1) {
                searchData.collect = true
            }
            if (searchData.collect === 0) {
                searchData.collect = false
            }
            delete searchData.createTime
            delete searchData.bidOpenDate
            this.loading = true

            findListCount(searchData).then(res => {
                if (res.code === this.SUCCESS_CODE) this.statistics = res.data
            })
            applyList(searchData).then(res => {
                if (res.code === 200) {
                    if (callback) {
                        if (res.data.length) {
                            let temp = this.$clone(res.data)
                            temp.map(v => {
                                if (!v.projectName) v.projectName = v.branchCompanyName
                                if (!v.projectCode) v.projectCode = v.branchCompanyCode
                                v.collect = v.collect ? '是' : '否'
                            })
                            callback(temp)
                        } else {
                            this.$message.warning('没有可导出的数据')
                        }
                        this.exportAllLoading = false
                        this.loading = false
                        return
                    }
                    _this.tableData = res.data
                    _this.totalSize = res.totalSize
                }
                this.loading = false
            }).catch(() => {
                this.loading = false
            })
        },
        search(value) {
            value && (this.queryParam = value)
            this.$refs.table.refresh(true)
        },
        handlePageChange(pageNo, pageSize) {
            this.pageNo = pageNo
            this.list()
            this.selectedRowKeys = []
        },
        showSizeChange(pageNo, pageSize) {
            this.pageNo = 1
            this.pageSize = pageSize
            this.list()
        },
        onSelectChange(selectedRowKeys, selectedRows) {
            let _this = this
            _this.selectId = []
            selectedRows.map(item => {
                // 选中id
                _this.selectId.push(item.id)
            })
            this.chooseData = selectedRowKeys.length
            _this.selectedRowKeys = selectedRowKeys
            _this.selectedRows = selectedRows
        },
        handleDel(id) {
            let _this = this
            let params = id ? [id] : _this.selectId
            _this.delList(applyListDel, () => {
                _this.search()
                this.selectedId = []
                this.selectedRowKeys = []
                this.selectedRows = []
                this.editingKey = ''
            }, { ids: params })

        },
        handleEdit(record, state) {
            this.$router.push({
                path: '/material/procurement/applyAdd',
                query: {
                    id: record && record.id || '',
                    state: state
                }
            })
        }
    }
}
</script>
